<?php

namespace app\common\model;

class UserWxModel extends BaseModel
{
    protected $name = 'user_wx'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /**
     * 绑定微信用户
     * @param $uid
     * @param $wxuser
     * @return bool
     * @throws \Exception
     */
    public function bindUser($uid,$wxuser){
        try{
            $wxinfo = $this->where('uid',$uid)->find();
            if(!$wxinfo){
                $wxuser['uid']=$uid;
                $this->isUpdate(false)->allowField(true)->save($wxuser);
            }else{
                if($wxinfo['openid']==$wxuser['openid']){
                    $this->isUpdate(true)->allowField(true)->save($wxuser,['uid'=>$uid]);
                }
            }
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
        return true;
    }
}